package com.genITeam.ria.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.genITeam.ria.actions.AbstractAction;

/**
 * Servlet implementation class for Servlet: AbstractController
 *
 */
 public abstract class AbstractController extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	 
	 // we have an object here, because we set a mutex on it
	    static private Long lastDeamonRun = new Long(-1);

	    // project root patch
	    protected File webAppPath;

	    protected HashMap actions;
	    
	public AbstractController() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
        String action = request.getParameter("action");
        if (action == null)
        {
            action = "index";
        }
        try
        {
            executeAction(request, response, action);
        }
        catch (Exception e)
        {
            throw new ServletException(e);
        }
        finally
        {
        	
        }
	}   	  	  
	
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	    actions = new HashMap();
	}   

    public AbstractAction getAction(String action) throws Exception
    {
        Class className = (Class) actions.get(action);
        if (className == null)
        {
            throw new Exception("action not found:" + action);
        }
        AbstractAction abstractAction = (AbstractAction) className.newInstance();
        return abstractAction;

    }

    /**
     * This method can be overloaded to perform authentication/daemon processess
     */
    public void executeAction(HttpServletRequest req, HttpServletResponse res, String action) throws Exception
    {
        AbstractAction a = getAction(action);
        executeAction(req, res, a);
    }
    

    private void executeAction(HttpServletRequest req, HttpServletResponse res, AbstractAction a) throws Exception
    {

        ServletContext servletContext = getServletContext();
        a.init(this, servletContext, req, res);
    //    a.execute();

    }

}